package com.smart.core.starter.aspect;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * web请求拦截器
 *
 * @author guxiaobai
 * @date 2018/12/5 14:27
 */
@Aspect
@Component
@Slf4j
public class WebLogAspect {

    @Pointcut("execution(public * com.smart..*Controller.*(..))")
    public void webLog() {
    }

    @Around("webLog()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 记录下请求内容
        String strClassName = joinPoint.getTarget().getClass().getName();
        String strMethodName = joinPoint.getSignature().getName();
        log.debug("[class]:{},[method]:{}", strClassName, strMethodName);
        Object[] args = joinPoint.getArgs();
        log.debug("[param] : {}", Arrays.toString(args));
        Long startTime = System.currentTimeMillis();
        Object obj = joinPoint.proceed();
        log.debug("response : {}", JSON.toJSONString(obj));
        log.debug("spendTime : {}", (System.currentTimeMillis() - startTime));
        return obj;
    }

}